package com.cjzuji.rental.util;

import com.cjzuji.rental.controller.order.YouPinController;
import com.cjzuji.rental.pojo.OrdersDetailDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
@Slf4j
public class OrdersUtil {
    private static final Logger logger = LoggerFactory.getLogger(OrdersUtil.class);
    public static File writeExcelToLocal(List<OrdersDetailDTO> dataList, String outputPath) throws IOException {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("订单数据");

            // 创建标题行
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("用户ID");
            headerRow.createCell(1).setCellValue("店铺Id");
            headerRow.createCell(2).setCellValue("平台ID");
            headerRow.createCell(3).setCellValue("订单ID");
            headerRow.createCell(4).setCellValue("下单用户名称");
            headerRow.createCell(5).setCellValue("用户电话");
            //user
            headerRow.createCell(6).setCellValue("地址详情");
            headerRow.createCell(7).setCellValue("身份证号");
            headerRow.createCell(8).setCellValue("身份证姓名");
            headerRow.createCell(9).setCellValue("产品备注");
            headerRow.createCell(10).setCellValue("产品属性");
            headerRow.createCell(11).setCellValue("产品SKU");
            headerRow.createCell(12).setCellValue("租赁开始时间");
            headerRow.createCell(13).setCellValue("租赁结束时间");
            headerRow.createCell(14).setCellValue("快递单号");
            headerRow.createCell(15).setCellValue("分配人");
            headerRow.createCell(16).setCellValue("还款日");
            headerRow.createCell(17).setCellValue("总租金");
            headerRow.createCell(18).setCellValue("买断金");
            headerRow.createCell(19).setCellValue("月租金");
            headerRow.createCell(20).setCellValue("碎屏金额");
            headerRow.createCell(21).setCellValue("碎屏状态");
            headerRow.createCell(22).setCellValue("订单总金额");
            headerRow.createCell(23).setCellValue("押金");
            headerRow.createCell(24).setCellValue("总租期");
            int BILL_HEADER_START_COL = 25;
            for (int i=0;i<18;i++){
                System.out.println("BILL_HEADER_START_COL"+BILL_HEADER_START_COL);
                headerRow.createCell(BILL_HEADER_START_COL++).setCellValue("租期:第" +(i+1)+ "期" );
                headerRow.createCell(BILL_HEADER_START_COL++).setCellValue("第"+(i+1)+"期"+"应还金额");
                headerRow.createCell(BILL_HEADER_START_COL++).setCellValue("第"+(i+1)+"期"+"还款状态");
                headerRow.createCell(BILL_HEADER_START_COL++).setCellValue("第"+(i+1)+"期"+"还款日");
                headerRow.createCell(BILL_HEADER_START_COL++).setCellValue("第"+(i+1)+"期"+"还款时间");
                headerRow.createCell(BILL_HEADER_START_COL++).setCellValue("第"+(i+1)+"期"+"逾期违约金");
            }

            // 填充数据
            int rowNum = 1;
            for (OrdersDetailDTO user : dataList) {
                System.out.println("user"+user.toString());
                Row row = sheet.createRow(rowNum++);
                row.createCell(0).setCellValue(user.getId());
                row.createCell(1).setCellValue(getShopName(user.getShopId()));
                row.createCell(2).setCellValue(getPlatformName(user.getPlatformId()));
                row.createCell(3).setCellValue(user.getOrderId());
                row.createCell(4).setCellValue(user.getRealName());
                row.createCell(5).setCellValue(user.getPhone());
                row.createCell(6).setCellValue(user.getAddressInfo());
                row.createCell(7).setCellValue(user.getIdCard());
                row.createCell(8).setCellValue(user.getIdCardName());
                row.createCell(9).setCellValue(user.getCarBrand());
                row.createCell(10).setCellValue(user.getColour());
                row.createCell(11).setCellValue(user.getSku());
                row.createCell(12).setCellValue(user.getRentStartTime());
                row.createCell(13).setCellValue(user.getRentLastTime());
                row.createCell(14).setCellValue(user.getDeliveryNo());
                row.createCell(15).setCellValue(user.getAssigner());
                row.createCell(16).setCellValue(user.getRentStartTime().substring(8,10));//字符串截取
                row.createCell(17).setCellValue(user.getActualFiancingAmount());
                row.createCell(18).setCellValue(user.getBuyoutAmount());
                row.createCell(19).setCellValue(user.getMonthRepayment());
                row.createCell(20).setCellValue(user.getScreenRisksAmount());
                row.createCell(21).setCellValue(getScreenRisksType(user.getScreenRisksType()));
                row.createCell(22).setCellValue(user.getBondFreeCreditAmt());
                row.createCell(23).setCellValue(user.getSurplusBondAmt());
                row.createCell(24).setCellValue(user.getLeaseTerm());
                final int[] setIndex = {25};  // 使用数组包装
                if(!user.getOrderBillList().isEmpty()){
                    user.getOrderBillList().forEach(billItem -> {
                        row.createCell(setIndex[0]++).setCellValue(billItem.getTerm());
                        row.createCell(setIndex[0]++).setCellValue(billItem.getCapital());
                        row.createCell(setIndex[0]++).setCellValue(billItem.getRepayStatusStr());
                        row.createCell(setIndex[0]++).setCellValue(billItem.getRepayDateFmt());
                        row.createCell(setIndex[0]++).setCellValue(billItem.getRealRepayTime());
                        row.createCell(setIndex[0]++).setCellValue(billItem.getOverdueAmount());
                    });
                }

            }

            // 自动调整列宽
            for (int i = 0; i < 4; i++) {
                sheet.autoSizeColumn(i);
            }
            // 创建输出文件
            File outputFile = new File(outputPath);
            try (FileOutputStream fos = new FileOutputStream(outputFile)) {
                workbook.write(fos);
            }

            return outputFile;
        }

    }

    //写个枚举例子,当user.getShopId()为1时，将其替换成广创
    public static String getShopName(int shopId) {
        switch (shopId) {
            case 1:
                return "广创";
            case 2:
                return "长九";
            case 3:
                return "稳实";
            case 4:
                return "谦莲";
            case 5:
                return "云链通";
            default:
                return "未知";
        }
    }
    //user.getPlatformId()
    public static String getPlatformName(int platformId) {
        switch (platformId) {
            case 1:
                return "优品";
            case 2:
                return "爱租机";
            case 3:
                return "人人租";
            case 4:
                return "零零";
            case 5:
                return "俏租机";
            case 6:
                return "爱豆";
            case 7:
                return "二手优品";
            case 8:
                return "二手人人租";
            case 9:
                return "X物";
            case 10:
                return "二店优品";
            default:
                return "未知";
        }
    }
    //screen_risks_type
    public static String getScreenRisksType(int screenRisksType) {
        switch (screenRisksType) {
            case 0:
                return "未支付";
            case 1:
                return "支付";
            default:
                return "未知";
        }
    }
}
